﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using DotNetDream.Extenstion;
using DotNetDream.Models;
using DotNetDream.Helper;

namespace DotNetDream.Controllers
{
    public class EntryController : DndController
    {
        /// <summary>
        /// Get the view of an entry
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ViewResult ViewEntry(int id)
        {
            BlogEntry entry = Database.BlogEntries.Find(id);
            if (entry != null)
                return View(entry);
            else
            {
                ViewBag.ErrorMessage = "Entry not found.";
                return View("Error");
            }
        }

        /// <summary>
        /// View entry postback. Can be a comment post or go back to blog
        /// </summary>
        /// <param name="command"></param>
        /// <param name="entry"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost, ValidateInput(false)]
        public ActionResult ViewEntry(string command, BlogEntry entry, string year, string month, string page)
        {
            //Back button click
            if (command.ToLower().Equals("back"))
            {
                return RedirectToAction("ViewBlog", "Blog", new { id = entry.BlogId, year = year, month = month, page = page });
            }
            //Comment button click
            else
            {
                //Try adding new comment
                try
                {
                    //Create new comment
                    BlogEntryComment comment = new BlogEntryComment();
                    comment.Created = DateTime.Now;
                    comment.Content = entry.NewComment;
                    MembershipUser user = Membership.GetUser(User.Identity.Name);
                    comment.OwnerId = (Guid)user.ProviderUserKey;
                    comment.BlogEntryId = entry.Id;

                    //Add to the DB then save
                    Database.BlogEntryComments.Add(comment);
                    Database.SaveChanges();

                    //TODO: Hard code mailing list
                    MailMaster mailer = new MailMaster();
                    mailer.Send("hugh.hoang@gmail.com", "New Comment posted!", string.Format(@"By: {0}<br>http://www.dotnetdream.com/Entry/ViewEntry/{1}", user.UserName, entry.Id));
                    mailer.Send("ngalph@gmail.com", "New Comment posted!", string.Format(@"By: {0}<br>http://www.dotnetdream.com/Entry/ViewEntry/{1}", user.UserName, entry.Id));

                    //Refresh the entry
                    return RedirectToAction("ViewEntry", new { id = entry.Id, year = year, month = month, page = page });
                }
                //Fail to add
                catch (Exception ex)
                {
                    ViewBag.ErrorMessage = ex.Message;
                    return View("Error");
                }
            }
        }

        /// <summary>
        /// Get a view to create an entry
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult CreateEntry()
        {
            return View();
        }

        /// <summary>
        /// Create entry postback
        /// </summary>
        /// <param name="command"></param>
        /// <param name="blogId"></param>
        /// <param name="entry"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost, ValidateInput(false)]
        public ActionResult CreateEntry(string command, string blogId, BlogEntry entry)
        {
            //Back button click
            if (command.ToLower().Equals("back"))
            {
                return RedirectToAction("ViewBlog", "Blog", new { id = blogId });
            }
            //Publish button click
            else
            {
                //Try adding new entry
                try
                {
                    //Populate required fields
                    entry.BlogId = int.Parse(blogId);
                    MembershipUser user = Membership.GetUser(User.Identity.Name);
                    entry.OwnerId = (Guid)user.ProviderUserKey;
                    entry.Created = DateTime.Now;
                    entry.Updated = DateTime.Now;
                    //Save the changes
                    Database.BlogEntries.Add(entry);
                    Database.SaveChanges();

                    //TODO: Hard code mailing list
                    MailMaster mailer = new MailMaster();
                    mailer.Send("hugh.hoang@gmail.com", "New Entry posted!", string.Format(@"By: {0}<br>http://www.dotnetdream.com/Entry/ViewEntry/{1}", user.UserName, entry.Id));
                    mailer.Send("ngalph@gmail.com", "New Entry posted!", string.Format(@"By: {0}<br>http://www.dotnetdream.com/Entry/ViewEntry/{1}", user.UserName, entry.Id));

                    //Navigate to newly created entry
                    return RedirectToAction("ViewEntry", new { id = entry.Id });
                }
                //Fail to add
                catch (Exception ex)
                {
                    ViewBag.ErrorMessage = ex.Message;
                    return View("Error");
                }
            }
        }
    }
}